package com.shadow.bus.main.complete;

import com.alibaba.fastjson.TypeReference;
import com.shadow.bus.config.ItemFiled;
import com.shadow.bus.config.ItemType;
import com.shadow.bus.entity.RepairEntity;
import com.shadow.bus.entity.TroubleEntity;
import com.shadow.bus.entity.TroubleOperationEntity;
import com.tepth.shadow.ui.recycler.MultipleItemEntity;
import com.tepth.shadow.ui.recycler.ObjConverter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author zhangyu
 * @date 2018/7/10
 */

public class CompleteDataConverter {

    public void convert(final String response, final OnConverterListener listener) {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> e) throws Exception {
                ArrayList<MultipleItemEntity> itemEntities = new ArrayList<>();
                RepairEntity repairEntity = new ObjConverter<RepairEntity>().
                        convert(response, new TypeReference<RepairEntity>() {
                        });
                if (repairEntity != null) {
                    List<TroubleEntity> troubleEntities = repairEntity.getListTrouble();
                    if (troubleEntities != null) {
                        for (TroubleEntity troubleEntity : troubleEntities) {
                            itemEntities.add(MultipleItemEntity.builder()
                                    .setItemType(ItemType.TYPE_ITEM_TROUBLE_CONTENT)
                                    .setField(ItemFiled.ITEM_OBJ, troubleEntity)
                                    .build());
                            List<TroubleOperationEntity> operationEntityList =
                                    troubleEntity.getListContent();
                            if ((operationEntityList == null || operationEntityList.isEmpty())) {
                                //除开附加作业项
                                if (!"自定义录入@@".equals(troubleEntity.getName())) {
                                    e.onError(new Exception("不能竣检"));
                                } else {
                                    itemEntities.remove(itemEntities.size() - 1);
                                }
                                break;
                            } else {
                                int reworkSize = 0;
                                for (TroubleOperationEntity entity : operationEntityList) {
                                    if (entity.isRework()) {
                                        reworkSize++;
                                        itemEntities.add(MultipleItemEntity
                                                .builder()
                                                .setItemType(ItemType.TYPE_ITEM_TROUBLE_OPERATOR_REWORK)
                                                .setField(ItemFiled.ITEM_OBJ, entity)
                                                .setField(ItemFiled.ITEM_TROUBLE_ID, troubleEntity.getId())
                                                .setField(ItemFiled.ITEM_TROUBLE_NAME, troubleEntity.getName())
                                                .build());
                                    } else {
                                        itemEntities.add(MultipleItemEntity
                                                .builder()
                                                .setItemType(ItemType.TYPE_ITEM_TROUBLE_OPERATOR_PUNCH)
                                                .setField(ItemFiled.ITEM_OBJ, entity)
                                                .setField(ItemFiled.ITEM_TROUBLE_ID, troubleEntity.getId())
                                                .setField(ItemFiled.ITEM_TROUBLE_NAME, troubleEntity.getName())
                                                .build());
                                    }
                                }
                                //全部都为返工
                                if (reworkSize == operationEntityList.size()) {
                                    //且不是附加作业项
                                    if (!"自定义录入@@".equals(troubleEntity.getName())) {
                                        e.onError(new Exception("不能竣检"));
                                    }
                                }
                            }
                        }
                    }
                }
                e.onNext(itemEntities);
            }
        })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    @SuppressWarnings("unchecked")
                    public void onNext(Object o) {
                        ArrayList<MultipleItemEntity> list = (ArrayList<MultipleItemEntity>) o;
                        if (listener != null) {
                            listener.onConverterAllData(list);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null) {
                            listener.canCompletePunch(false);
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public interface OnConverterListener {
        void onConverterAllData(ArrayList<MultipleItemEntity> list);

        void canCompletePunch(boolean isComplete);

    }
}
